<template>
  <div class="app-container">
    <div class="search-container">
      <el-form :model="searchParam" :inline="true">
        <el-form-item label="供应商名称">
          <el-input
            v-model="searchParam.partnerName"
            placeholder="请输入供应商名称"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="供应商组名称">
          <el-input
            v-model="searchParam.partnerGroupingName"
            placeholder="请输入供应商组名称"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="reset"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <template #header>
        <el-button type="success" plain @click="openDialog()"
          ><i-ep-plus />新增</el-button
        >
        <el-button
          type="danger"
          plain
          :disabled="ids.length === 0"
          @click="handleTableDelete()"
          ><i-ep-delete />删除</el-button
        >
      </template>

      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column
          label="供应商编号"
          prop="partnerCode"
          min-width="150"
          header-align="center"
          show-overflow-tooltip
        />
        <el-table-column
          label="供应商名称"
          prop="partnerName"
          min-width="200"
          header-align="center"
          show-overflow-tooltip
        />
        <el-table-column
          label="供应商组"
          prop="partnerGroupingName"
          min-width="200"
          header-align="center"
          show-overflow-tooltip
        />
        <el-table-column
          label="所属公司编号"
          prop="organizationCode"
          min-width="180"
          header-align="center"
          show-overflow-tooltip
        />
        <el-table-column
          label="所属公司名称"
          prop="organizationName"
          min-width="200"
          header-align="center"
          show-overflow-tooltip
        />
        <el-table-column
          label="国家"
          prop="country"
          min-width="100"
          align="center"
          show-overflow-tooltip
        />
        <el-table-column
          label="地区"
          prop="area"
          min-width="260"
          header-align="center"
          show-overflow-tooltip
        >
          <template #default="scope">
            {{
              trim(
                trim(scope.row.province) +
                  "" +
                  trim(scope.row.city) +
                  "" +
                  trim(scope.row.region)
              )
            }}
          </template>
        </el-table-column>
        <el-table-column
          label="详细地址"
          prop="addressDesc"
          min-width="350"
          header-align="center"
          show-overflow-tooltip
        />
        <el-table-column
          label="备注"
          prop="remarks"
          min-width="200"
          header-align="center"
          show-overflow-tooltip
        />
        <el-table-column
          label="修改时间"
          prop="lastUpdateTime"
          min-width="160"
          align="center"
          show-overflow-tooltip
        />
        <el-table-column
          label="修改人"
          prop="lastUpdatedUserName"
          min-width="120"
          align="center"
          show-overflow-tooltip
        />
        <el-table-column fixed="right" label="操作" width="360" align="center">
          <template #default="scope">
            <el-tooltip
              :hide-after="0"
              effect="light"
              content="查看联系人"
              placement="top"
            >
              <el-button
                type="primary"
                size="small"
                link
                @click="openContactDialog(scope.row)"
                ><i-ep-view
              /></el-button>
            </el-tooltip>
            <el-tooltip
              :hide-after="0"
              effect="light"
              content="查看供应产品"
              placement="top"
            >
              <el-button
                type="primary"
                size="small"
                link
                @click="openProductDialog(scope.row)"
                ><i-ep-view
              /></el-button>
            </el-tooltip>
            <el-tooltip
              :hide-after="0"
              effect="light"
              content="编辑"
              placement="top"
            >
              <el-button
                type="primary"
                size="small"
                link
                @click="openDialog(scope.row)"
                ><i-ep-edit
              /></el-button>
            </el-tooltip>
            <el-tooltip
              :hide-after="0"
              effect="light"
              content="删除"
              placement="top"
            >
              <el-button
                type="primary"
                size="small"
                link
                @click="handleTableDelete(scope.row.id)"
                ><i-ep-delete
              /></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        :background="true"
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[10, 25, 50, 100]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <partner-edit
      :visible="addDialog.visible"
      :title="addDialog.title"
      :data="addDialog.data"
      @reset-table="(id: string) => (id?.length ? getTableList() : reset())"
      @change-visible="changeVisible"
    />

    <mdm-partner-contact-edit
      :visible="contactDialog.visible"
      :title="contactDialog.title"
      :data="contactDialog.data"
      @change-visible="changeContactVisible"
    />

    <mdm-partner-product-edit
      :visible="productDialog.visible"
      :title="productDialog.title"
      :data="productDialog.data"
      @change-visible="changeProductVisible"
    />
  </div>
</template>

<script setup lang="ts">
import { useTable, useDownload, useDictionary } from "@/utils/useTable";
import request from "@/utils/request";

defineOptions({
  name: "MDMPARTNER",
  inheritAttrs: false,
});

// 请求参数
const requestApi = "/mdm/mdmpartner/queryByPage";

// 是否需要分页
const pagination = true;

const initParam = {
  sort: {
    column: "last_update_time",
    orderBy: "desc",
  },
};

// 初始化搜索参数数据
const searchParamInit = {
  partnerName: "",
  partnerGroupingName: "",
};

const deleteParam = {
  api: "/mdm/mdmpartner/delete",
  data: {},
};

// 数据请求错误后处理
function requestError() {}

const dataCallBack = ref(undefined); // 无需二次数据处理&不传赞数也行

// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  ids,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  handleTableDelete,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value,
  requestError,
  deleteParam
);

onMounted(() => {
  getTableList();
});

const addDialog = reactive<DialogOption>({
  visible: false,
  title: "",
  data: {},
});

const contactDialog = reactive<DialogOption>({
  visible: false,
  title: "",
  data: {},
});

const productDialog = reactive<DialogOption>({
  visible: false,
  title: "",
  data: {},
});

// 查询详情
async function getInfo(id: string) {
  request({
    url: `/mdm/mdmpartner/get/${id}`,
    method: "get",
  })
    .then((data) => {
      addDialog.data = data.data;
      addDialog.visible = true;
    })
    .catch((res) => {
      console.log(res);
    });
}

/** 打开供应商/客户表单弹窗 */
function openDialog(roleObj?: Object) {
  if (roleObj) {
    addDialog.title = "修改供应商/客户";
    getInfo(roleObj.id);
  } else {
    addDialog.title = "新增供应商/客户";
    addDialog.visible = true;
  }
}

function changeVisible(value: boolean) {
  addDialog.visible = value;
  if (!value) {
    addDialog.data = {};
  }
}

/** 打开供应商/客户表单弹窗 */
function openContactDialog(roleObj?: Object) {
  contactDialog.title = "查看联系人";
  getContactInfo(roleObj.id);
}

// 查询详情
async function getContactInfo(id: string) {
  request({
    url: "/mdm/partnercontact/getList/",
    method: "post",
    data: {
      partnerId: id,
    },
  })
    .then((data) => {
      contactDialog.data = data.data;
      contactDialog.visible = true;
    })
    .catch((res) => {
      console.log(res);
    });
}

function changeContactVisible(value: boolean) {
  contactDialog.visible = value;
  if (!value) {
    contactDialog.data = [];
  }
}

/** 打开供应商/客户表单弹窗 */
function openProductDialog(roleObj?: Object) {
  productDialog.title = "查看供应产品";
  getMaterialsInfo(roleObj.id);
}

// 查询详情
async function getMaterialsInfo(id: string) {
  request({
    url: `/mdm/partnerproduct/materialList/${id}`,
    method: "get",
  })
    .then((data) => {
      productDialog.data = data.data;
      productDialog.visible = true;
    })
    .catch((res) => {
      console.log(res);
    });
}

function changeProductVisible(value: boolean) {
  productDialog.visible = value;
  if (!value) {
    productDialog.data = [];
  }
}

function trim(val) {
  if (val) {
    return val;
  } else {
    return "";
  }
}
</script>
